//<SCRIPT language="javascript">
var PAGE_NUM_NA = -1;
var PAGES_ALL = -1;
var PC_FROM = 0;
var PC_TO = 1;
var MSG_NO_APPL_ID = "No ApplicantID";
var imaging = null;//Web Service short cut
var uo = null;//Web Service short cut
var selHist = new SelectHistory();
var ff = new Files({onclick:clickFile  ,ondragstart:fileDragStart,ongragging:folderDrag,ondrop:fileDrop,drawPages:drawPages,__histObj:selHist,_folderID:ff_folderID});//Faxes
var ii = new Files({onclick:clickFolder,ondragstart:fileDragStart,ondragend:dragEnd,drawPages:drawFolderPages,ongragging:folderDrag,ondrop:folderDrop,__histObj:selHist,_folderID:ii_folderID});//Images
var featuresMail = "dialogHeight:300px; dialogWidth:400px; resizable:1; status:0; help:0";
function load_Page(){//debugger
	svcApplInfo.onserviceavailable = onWebServiceAvailable
	svcApplInfo.useService("Imaging.asmx?WSDL","Imaging");
	if( typeof(load_Page_NS) != 'undefined' )load_Page_NS();//execute custom onload code
//	setCookie("D~NameSpace",top.g_nameSpace);
	//Locals
	function onWebServiceAvailable(){
		imaging = svcApplInfo.Imaging;
	}
}
//******* Dragin' ********
	//***** Common *****
var dragDefault;
var DRAG_MOVE = 0;
var DRAG_COPY = 1;
var DRAG_ACTIONS = new Array("move","copy");
function startMove(){
	_dragData(event.srcElement.uniqueID);
	window.event.dataTransfer.effectAllowed = "all";
	_dropEffect(dragDefault=DRAG_MOVE);
}
function _dragData(text){
	if( !defined(text) )return event.dataTransfer.getData("text");
	event.dataTransfer.setData("text",text);
}
function _dragElement(){return eval(_dragData())}
function _dragCopy(){return event.ctrlKey};
function _dropEffect_Auto(){
	_dropEffect(_dragCopy()?dragDefault^1:dragDefault);
}
function _dropEffect(ef){
	if( defined(ef) )event.dataTransfer.dropEffect = DRAG_ACTIONS[ef];
//	return status = event.dataTransfer.dropEffect;
}
function fileDragStart(){
	startMove();
}
function dragEnd(){
//	this._select(1);
}
	//***** File ******
function fileDrop(){
this.moveWithinFile = moveWithinFile;
this.moveFileToFile = moveFileToFile;
this.moveFolderToFile = moveFolderToFile;
var elFrom = _dragElement();
var elTo = event.srcElement;
var copy = _dragCopy();
	clearSelection();
	if( this._pagesCont(elTo) == this._pagesCont(elFrom) )return this.moveWithinFile(elTo,elFrom);
	if( this._container(elTo) == this._container(elFrom) )return this.moveFileToFile(elTo,elFrom);
	return this.moveFolderToFile(elTo,elFrom);
	///Locals
	function moveFileToFile(elTo,elFrom){
		var contFrom = this._fileCont(elFrom);
		var fileTo = this._fileName(elTo);
		var fileFrom = this._fileName(elFrom);
		var pageAfter = this._isPage(elTo)?this._pageNum(elTo):PAGE_NUM_NA;
		var pageCount = this._isPage(elFrom)?1:PAGES_ALL;
		var pageStart = this._isPage(elFrom)?this._pageNum(elFrom):0;
		var co = svcApplInfo.createCallOptions("MoveFileToFile",undefined,false);
		var result = svcApplInfo.Imaging.callService(co,fileFrom,fileTo,pageAfter,pageStart,pageCount,copy);
		if( showSOAPError(result) )return;
		var pc = result.value;
		this.drawPages(elFrom,pc[PC_FROM]);
		if( pc[PC_FROM] == 0 )this.deleteFile(contFrom);
		this.drawPages(elTo,pc[PC_TO]);
		var pageShow = pageAfter+1;
		this.select(this._pageNum(elTo,pageShow));
		setTimeout(function(){showFilePages(fileTo,pageShow,pageCount)},100);
	}
	function moveFolderToFile(elTo,elFrom){
		var contFrom = this._fileCont(elFrom);
		var fileTo = this._fileName(elTo);
		var folderFrom = this._fileCont(elFrom).folderID;
		var pageAfter = this._isPage(elTo)?this._pageNum(elTo):PAGE_NUM_NA;
		var pageCount = this._isPage(elFrom)?1:PAGES_ALL;
		var pageStart = this._isPage(elFrom)?this._pageNum(elFrom):0;
		var co = svcApplInfo.createCallOptions("MoveFolderToFile",undefined,false);
		var result = svcApplInfo.Imaging.callService(co,_applID(),folderFrom,fileTo,pageAfter,pageStart,pageCount,copy);
		if( showSOAPError(result) )return;
		this.drawPages(elFrom,result.value[PC_FROM]);
		this.drawPages(elTo,result.value[PC_TO]);
		var pageShow = pageAfter+1;
		this.select(this._pageNum(elTo,pageShow));
		setTimeout(function(){showFilePages(fileTo,pageShow,pageCount)},100);
	}
	function moveWithinFile(elTo,elFrom){
		var fileName = this._fileName(elTo);
		var pageAfter = this._isPage(elTo)?this._pageNum(elTo):PAGE_NUM_NA;
		var pageCount = this._isPage(elFrom)?1:PAGES_ALL;
		var pageStart = this._isPage(elFrom)?this._pageNum(elFrom):0;
		var co = svcApplInfo.createCallOptions("MoveWithinFile",undefined,false);
		var result = svcApplInfo.Imaging.callService(co,fileName,pageAfter,pageStart,pageCount);
		if( showSOAPError(result) )return;
		var pageShow = pageAfter+1-pageCount+(pageAfter<pageStart?1:0);
		this.select(this._pageNum(elTo,pageShow));
		setTimeout(function(){showFilePages(fileName,pageShow,pageCount)},1000);
	}
}
	//***** Folder ******
function folderDrag(){
	var elTo = event.srcElement;
	var elFrom = _dragElement();
  if( elTo == elFrom )return;
	var	typeFrom = this._type(elFrom);
  if( typeFrom == this._typeNA )return;
	var	typeTo = this._type(elTo);
  if( typeTo == this._typeNA )return;
  var isBrother = this._isBrother(elTo,elFrom);
  if( typeFrom == this.__typePage && typeTo == typeFrom && isBrother && this._pageNum(elTo) == this._pageNum(elFrom)-1 )return;
	event.returnValue = false;
	_dropEffect_Auto();
}
function folderDrop(){
this.moveFileToFolder = moveFileToFolder;
this.moveFolderToFolder = moveFolderToFolder;
this.moveWithinFolder = moveWithinFolder;
var elFrom = _dragElement();
var elTo = event.srcElement;
var copy = _dragCopy();
	clearSelection();
	if( this._container(elTo) != ii._container(elFrom) )return this.moveFileToFolder(elTo,elFrom);
	if( this._pagesCont(elTo) == this._pagesCont(elFrom) )return this.moveWithinFolder(elTo,elFrom);
	if( this._container(elTo) == ii._container(elFrom) )return this.moveFolderToFolder(elTo,elFrom);
	///Locals
	function moveFolderToFolder(elTo,elFrom){
		var folderTo = this._fileCont(elTo).folderID;
		var folderFrom = this._fileCont(elFrom).folderID;
		var pageAfter = this._isPage(elTo)?this._pageNum(elTo):PAGE_NUM_NA;
		var pageCount = this._isPage(elFrom)?1:PAGES_ALL;
		var pageStart = this._isPage(elFrom)?this._pageNum(elFrom):0;
		if( pageCount == 0 )return alert("Source folder is empty!");
		var co = svcApplInfo.createCallOptions("MoveFolderToFolder",undefined,false);
		var result = svcApplInfo.Imaging.callService(co,_applID(),folderTo,folderFrom,pageAfter,pageStart,pageCount,copy);
		if( showSOAPError(result) )return;
		this.drawPages(elTo,result.value[PC_TO]);
		this.drawPages(elFrom,result.value[PC_FROM]);
		var pageShow = pageAfter+1;
		this.select(this._pageNum(elTo,pageShow));
		setTimeout(function(){showApplPages(_applID(),folderTo,pageShow,pageCount)},1000);
	}
	function moveWithinFolder(elTo,elFrom){
		var folderID = this._fileCont().folderID;
		var pageAfter = this._isPage()?this._pageNum():PAGE_NUM_NA;
		var pageCount = this._isPage(elFrom)?1:PAGES_ALL;
		var pageStart = this._isPage(elFrom)?this._pageNum(elFrom):0;
		var co = svcApplInfo.createCallOptions("MoveWithinFolder",undefined,false);
		var result = svcApplInfo.Imaging.callService(co,_applID(),folderID,pageAfter,pageStart,pageCount);
		if( showSOAPError(result) )return;
		var pageShow = pageAfter+1-pageCount+(pageAfter<pageStart?1:0);
		this.select(this._pageNum(elTo,pageShow));
		setTimeout(function(){showApplPages(_applID(),folderID,pageShow,pageCount)},1000);
	}
	function moveFileToFolder(elTo,elFrom){
		var folderID = this._fileCont().folderID;
		var pageAfter = this._isPage(elTo)?this._pageNum(elTo):PAGE_NUM_NA;
		var pageCount = ff._isPage(elFrom)?1:PAGES_ALL;
		var pageStart = ff._isPage(elFrom)?ff._pageNum(elFrom):0;
		var fileName = ff._fileName(elFrom);
		var co = svcApplInfo.createCallOptions("MoveFileToFolder",undefined,false);
		var result = svcApplInfo.Imaging.callService(co,_applID(),fileName,pageStart,pageCount,folderID,pageAfter,copy);
		if( showSOAPError(result) )return;
		if( result.value[PC_FROM] == 0 )ff.deleteFile(elFrom);
		else ff.drawPages(elFrom,result.value[PC_FROM]);
		this.drawPages(elTo,result.value[PC_TO]);
		this.select(this._pageNum(elTo,pageAfter+1));
		setTimeout(function(){showApplPages(_applID(),folderID,pageAfter+1,pageCount)},1000);
	}
}
//******* Database *******
function addApplicant(fileName,pageID){
var co = svcApplInfo.createCallOptions("AddApplicant",undefined,false);
var result = svcApplInfo.Imaging.callService(co,fileName,pageID,1);
	if( showSOAPError(result) )return;
	_applID(result.value);
}
// Pages
function hideFolders(){
	ii.showFiles(divFolders,false);
}
function showFilePages(fileName,pageNum,pageCount){
	if( typeof(fileName) == "object" ){
		pageCount = ff._isPage(fileName) ? 1 : PAGES_ALL;
		pageNum   = ff._isFile(fileName) ? 0 : ff._pageNum(fileName);
		fileName  = ff._fileName(fileName);
	}
var o = {fileName:fileName,pageNum:pageNum,pageCount:pageCount||1};
var src = "Imaging.asmx/ShowFilePages?"+makeQueryString(o);
	showImage(src);
}
function showApplPages(applID,folderID,pageNum,pageCount){
	if( typeof(applID) == "object" ){
		var e = applID;
		pageCount = ii._isPage(e) ? 1 : PAGES_ALL;
		pageNum   = ii._isFile(e) ? 0 : ii._pageNum(e);
		folderID = ii._folderID(e);
		applID = _applID();
	}
	if( !applID )return alert("No ApplicantID!");
	var o = {applID:applID,folderID:folderID,pageNum:pageNum,pageCount:pageCount};
	var src = "Imaging.asmx/ShowApplPages?"+makeQueryString(o);
	showImage(src);
}
function makeQueryString(o){
var i,a=new Array();
	for( i in o )
		a.push(i+"="+escape(o[i]));
	return a.join("&");
}
function showImage(src){
	var sFrame = elIfrmPageParent.innerHTML;
	elIfrmPageParent.innerHTML = "";
	var src = 'src="'+httpEscape(src)+'"';
	sFrame = sFrame.replace(/src=\".*\"/i,src);
	elIfrmPageParent.innerHTML = sFrame;
	///Locals
	function httpEscape(s){return s;
		return s.replace(/[?]+([^?]+)$/,function($0,$1){return "?"+escape($1)}) }
}
function deleteApplPage(e){
	if( !_applID() )return alert("No ApplicantID!");
	e = getContextElement(event.srcElement);
	if( !confirm("Delete page(s) from \""+ii._fileName(e)+"\"?") )return;
	var folderID	= ii._folderID(e);
	var pageCount = ii._isPage(e) ? 1 : PAGES_ALL;
	var pageNum		= ii._isFile(e) ? 0 : ii._pageNum(e);
	var co = svcApplInfo.createCallOptions("DeleteApplPage",undefined,false);
	var result = svcApplInfo.Imaging.callService(co,_applID(),folderID,pageNum,pageCount);
	if( showSOAPError(result) )return;
	var pages = ii.drawPages(e,result.value);
	var pg = Math.min(result.value-1,pageNum);
	if( pg < 0 )return;
	ii.select(ii._pageNum(pages,pg));
	showApplPages(_applID(),folderID,pg,1);
}
//******* Context Menus *******
function menuOut(){return;
	if( event.propertyName != "style.display" )	return;
	if( event.srcElement.style.display == "" )return;
	event.srcElement.contextFile.select(event.srcElement.contextElement,true);
}
	/****** Folder ******/
function menu_Page_Appl(e,offsetx){
	e = e || event.srcElement;
//	ii._select(null,true);
//	ff._select(null,true);
	var pageNum = ii._isPage(e)?ii._pageNum(e):0;
	var pageCount = ii._isPage(e)?1:PAGES_ALL;
	var menu = new MenuIE5();
	menu.addMenu("E-Mail","mailFolder("+ii._fileCont(e).folderID+","+pageNum+","+pageCount+")");
	menu.addMenu("-","");
	menu.addMenu("Copy to New File","newFileFromFolder("+ii._fileCont(e).folderID+","+pageNum+","+pageCount+",true)");
	menu.addMenu("Move to New File","newFileFromFolder("+ii._fileCont(e).folderID+","+pageNum+","+pageCount+")");
	menu.addMenu("-","");
	menu.addMenu("Delete","deleteApplPage()");
	menu.draw(menuMenus);
	showMenu(menuMenus,null,offsetx||(ii._isPage(e)?e.offsetLeft:1));
	menuMenus.contextFile = ii;
	event.cancelBubble = true;
	return false;
}
function newFileFromFolder(folderID,pageNum,pageCount,copy){
	if( !_applID() )return alert(MSG_NO_APPL_ID);
	if( !pageCountFolder(_applID(),folderID) )return alert("Sorry!\nThis folder is empty.");;
	var el = ff._pagesCont(getContextElement(event.srcElement));
	var newFile = trim(prompt((copy?"Copy":"Move")+" page(s) to file:","")||"");
	if( !newFile )return;
	var co = svcApplInfo.createCallOptions("NewFileFromFolder",undefined,false);
	var result = svcApplInfo.Imaging.callService(co,_applID(),newFile,folderID,pageNum,pageCount,copy);
	if( showSOAPError(result) )return;
	ii.deletePages(el,ii._pageCount(el) - result.value);
	getImageList();
}
	/***** File *****/
function menu_Page(){
	var e = event.srcElement;
	ff.select(e);
	var fileName = ff._fileName(e);
	var pageNum = ff._isPage(e)?ff._pageNum(e):PAGE_NUM_NA;
	var pageCount = 1;
	var del = " Page";
	if( pageNum == PAGE_NUM_NA ){
		pageNum = 0;
		pageCount = PAGES_ALL;
		del = " File";
	}
	var menu = new MenuIE5();
	menu.addMenu("E-Mail","mailFile  ('"+fileName+"',"+pageNum+","+pageCount+")");
	menu.addMenu("-","");
	menu.addMenu("Copy to New File","newFileFromFile('"+fileName+"',"+pageNum+","+pageCount+",true)");
	menu.addMenu("Move to New File","newFileFromFile('"+fileName+"',"+pageNum+","+pageCount+")");
	menu.addMenu("-","");
	menu.addMenu("Delete"+del,"deleteFile('"+fileName+"',"+pageNum+","+pageCount+")");
	//		menu.addMenu("Show Page","showFilePages('"+fileName+"',"+pageNum+","+pageCount+")");
	if( typeof(menu_Page_V) )menu_Page_V(menu);
	menu.draw(menuMenus);
	showMenu(menuMenus,null,1);
	menuMenus.contextFile = ff;
	event.cancelBubble = true;
	return false;
}
function newFileFromFile(fileName,pageNum,pageCount,copy){
	var newFile = trim(prompt((copy?"Copy":"Move")+" page(s) to file:",fileName)||"");
	if( !newFile )return;
	var co = svcApplInfo.createCallOptions("NewFileFromFile",undefined,false);
	var result = svcApplInfo.Imaging.callService(co,newFile,fileName,pageNum,pageCount,copy);
	if( showSOAPError(result) )return;
	getImageList();
}
function deleteFile(fileName,pageNum,pageCount){
	var el = ff._pagesCont(getContextElement(event.srcElement));
	if( !confirm(_msg(fileName,pageNum,pageCount)) )return;
	var co = svcApplInfo.createCallOptions("DeleteFile",undefined,false);
	var result = svcApplInfo.Imaging.callService(co,fileName,pageNum,pageCount);
	if( showSOAPError(result) )return;
	if( result.value == 0 )return	ff.deleteFile(el);
	else ff.drawPages(el,result.value);
	var pc = Math.min(pageNum,result.value-1);
	setTimeout(function(){showFilePages(ff._pageNum(el,pc))},100);
	///Locals
	function _msg(fileName,pageNum,pageCount){
		if( pageNum == PAGE_NUM_NA || pageNum == 0 && pageCount == PAGES_ALL )return "Delete "+fileName+" from the list.";
		return "Delete page "+(pageNum+1)+" from "+fileName+".";
	}
}
function mailFile(fileName,pageNum,pageCount){
	var o = showModalDialog("MailSelect.aspx",null,featuresMail);
	if( !o || !o.to )return;
	var co = svcApplInfo.createCallOptions("MailFile",undefined,false);
	var result = svcApplInfo.Imaging.callService(co,o.to,o.subject||"",fileName,pageNum,pageCount);
	showSOAPError(result);
}
function mailFolder(folderID,pageNum,pageCount){
	if( !_applID() )return alert(MSG_NO_APPL_ID);
	var o = showModalDialog("MailSelect.aspx",null,featuresMail);
	if( !o || !o.to )return;
	var co = svcApplInfo.createCallOptions("MailFolder",undefined,false);
	var result = svcApplInfo.Imaging.callService(co,o.to,o.subject||"",_applID(),folderID,pageNum,pageCount);
	showSOAPError(result);
}
function getImageList(){
var co = svcApplInfo.createCallOptions("GetImageList",undefined,false);
var result = svcApplInfo.Imaging.callService(co);
	if( showSOAPError(result) )return;
	divFiles.innerHTML = result.value;
}
function folderMenu(){
var e = ii.getFileNode();if(!e)return;
	mnuFolder.menuShow(true,getMenuX(),event.y);
	event.cancelBubble = true;
	return false;
}
function deleteApplFolder(mnu){
var e =	mnu.contextElement;	mnu.hide();
	if( !_applID() )return alert("ApplicantID is empty!");
	if( !confirm("Empty folder \""+ii._fileName(e)+"\"?") )return;
var folderID = ii._fileCont(e).folderID;
var co = svcApplInfo.createCallOptions("DeleteApplPage",undefined,false);
var result = svcApplInfo.Imaging.callService(co,_applID(),folderID,PAGE_NUM_NA,0);
	if( showSOAPError(result) )return;
	ii.deletePages(e);
	ii.showPages(e,false);
	ii.onclick(e);
}
//******* Event handlers *******
//*********************** Folder ***********************************
function clickFolder(e,pageStart){
	var prevSel = selHist.Get();
	var e = this.select();
	if( !e )return;
	if( this._isFile(e) ){
		var _oldFile = this._oldFile;
		setTimeout(function(){_oldFile(e)},100);
		if( this.showPages(e) && this._pageCount(e) != 0 ){
			selHist.Pop(this.select(e,true));
			this.select(prevSel);
			return this.showPages(e,false);
		}
		this.drawPages(e,pageCountFolder(_applID(),this._folderID(e)));
	}
	showApplPages(e);
	///Locals
}
function pageCountFolder(applID,folderID){
	if( !_applID() )return alert(MSG_NO_APPL_ID) || 0;
	var result = svcApplInfo.Imaging.callService(svcApplInfo.createCallOptions("PageCount_Appl",undefined,false),applID,folderID);
	return showSOAPError(result)?0:result.value;
}
function drawFolderPages(e,count,folderID,add){
	if( !count && count != 0 )return;
	if( defined(add) ) return alert("[add] parameter is used in drawFolderPages(...)");
	if( defined(folderID) ) return alert("[folderID] parameter is used in drawFolderPages(...)");
	var folderID = this._folderID(e);
	var pc = this._pageCount(e);
	for( var i=pc; i < count; i++ ){
		var div = document.createElement("DIV");
		div.innerText = ++pc;
		div.className = "Page";
		div.oncontextmenu = function(){return menu_Page_Appl(this)};
//		div.onmouseup = new Function("return showApplPages(_applID(),"+folderID+","+(pc-1)+",1)");
		this.appendPage(e,div);
	}
	for( var pages = this._pagesCont(e) ;this._pageCount(pages) > count; this.deletePage(pages) ){}
	this.showPages(pages,true);
	return pages;
}
//********************************************************************
function getMenuX(){return event.x-getOffsetLeftByTag("td")-event.offsetX}
function getOffsetLeftByTag(parentTag,e){
	e = e || event.srcElement;
	for( var i = 0; e.parentElement.tagName.toLowerCase() != parentTag.toLowerCase(); e = e.parentElement )
		i += e.offsetLeft;
	return i
}
function clickFile(){
	var prevSel = selHist.Get();
	var e = this.select();
	if( !e )return;
	if( this._isFile(e) ){
		var _oldFile = this._oldFile;
		setTimeout(function(){_oldFile(e)},100);
		if( this.showPages(e) ){
			selHist.Pop(this.select(e,true));
			this.select(prevSel);
			return this.showPages(e,false);
		}
		var pages = pageCountFile(this._fileName(e));
		this.drawPages(this._pagesCont(e),pages);
	}
	showFilePages(e);
}
function pageCountFile(fileName){
var co = svcApplInfo.createCallOptions("PageCount",undefined,false);
var result = svcApplInfo.Imaging.callService(co,fileName);
	if( showSOAPError(result) )return 0;
	return result.value;
}
function drawPages(e,count){
	var fileName = this._fileName(e);
	var pc = this._pageCount(e);
	for( var i=pc; i < count; i++ ){
		var div = document.createElement("DIV");
		div.innerText = ++pc;
		div.className = "Page";
		div.oncontexmenu = menu_Page;
//		div.onmouseup = new Function("return showFilePages('"+fileName+"',"+(pc-1)+")");
		this.appendPage(e,div);
	}
	for( var pgCont=this._pagesCont(e) ;this._pageCount(pgCont) > count; this.deletePage(pgCont) ){}
	e.style.display = "";
	return pgCont;
}
//****** Virtual event handlers ******
function applID_changed_V(text){
	initFolders();
	return applID_changed(text);
}
	//**** Abstracts ****//
function applID_changed(){}
//****** Files object extentions ******
function ff_folderID(e){return};
function ii_folderID(e){return this._fileCont(e).folderID};
function initFolders(){
	ii.select(0,true);
	ii.showFiles(divFolders,false);
	showImage("about:blank");
}
//****** Properties ******
function _applID(v){
	if( defined(v) ){
		F.txtApplID.value = v;
		if( F.txtApplID.onchange )F.txtApplID.onchange();
	}
	return F.txtApplID.value || 0;
}
//****** Common ******
function clearSelection(){setTimeout("document.selection.empty();",100);}
//</SCRIPT>